$(document).ready(function () {
    //var teNum = getUrlParms("teNum");
    conditions();
    init();
    queryConditions();

})

//动态查询的条件生成
var conditions=function () {
    $.ajax({
        url: "http://localhost:8080/partyMember/queryConditions",
        type: "GET",
        async: true,
        dataType: "json",
        success: function (data) {
            //console.log(data);
            console.log(data.data);

            var grade=data.data.grade;
            var partyName=data.data.partyName;
            console.log("grade="+grade);
            console.log("partyName="+partyName);

            $.each(partyName,function(index,obj){
                console.log(obj.valueOf());
                $("#queryParty").append(" <option value='"+obj.valueOf()+"'>"+obj.valueOf()+"</option>");
            })

            $.each(grade,function(index,obj){
                console.log(obj.valueOf());
                $("#queryGrade").append(" <option value='"+obj.valueOf()+"'>"+obj.valueOf()+"</option>");
            })

        },
        error:function () {
            layer.msg("网络在开小差~稍后重试");
        }
    });
}

//初始化
var init=function(){
    $.ajax({
        url: "http://localhost:8080/partyMember/queryPON",
        type: "GET",
        async: false,
        data:{"partyMemberAccount":"1011"},
        dataType: "json",
        contentType:"application/json",
        success:function (data) {
            console.log("PONdata="+data);
            console.log("PONdata.data="+data.data);
            var PON=data.data.PON;
            console.log("PON="+PON);
            $('#dataTable').dataTable({
                "bDeferRender": true,
                "sAjaxDataProp": "data.partyMemberPartyStudentList",
                "sAjaxSource": "http://localhost:8080/partyMember/queryPartyMemberByConditions",
                "sPaginationType": "bs_full", //"bs_normal", "bs_two_button", "bs_four_button", "bs_full"
                "fnPreDrawCallback": function (oSettings) {
                    $('.dataTables_filter input').addClass('form-control input-large').attr('placeholder', 'Search');
                    $('.dataTables_length select').addClass('form-control input-small');
                },
                "oLanguage": {
                    "sSearch": "",
                    "sLengthMenu": "<span>_MENU_ entries</span>",
                    "sZeroRecords": "暂无相关信息",
                },
                "bJQueryUI": false,
                "bAutoWidth": false,
                "sDom": "<'row'<'col-lg-6 col-md-6 col-sm-12 text-center'l><'col-lg-6 col-md-6 col-sm-12 text-center'f>r>t<'row-'<'col-lg-6 col-md-6 col-sm-12'i><'col-lg-6 col-md-6 col-sm-12'p>>",
                // "fnServerData": function (fnCallback ) {
                //     $.ajax( {
                //         "dataType": "json",
                //         "type": "POST",
                //         "url": sSource,
                //     "data": aoData,
                //     "success": fnCallback
                //         } );
                //     },
                "aoColumns": [
                    {"mDataProp":"stuNum"},
                    {"mDataProp":"stuName"},
                    {"mDataProp":"stuSex",
                        "mRender": function(data) {
                            var value = "男";
                            if (data==1){
                                value = "女";
                            }
                            return value;
                        }
                    },
                    {"mDataProp":"stuNativeplace"},
                    {"mDataProp":"stuBirthday"},
                    {"mDataProp":"stuGrade"},
                    {"mDataProp":"partyMemberType",
                        "mRender": function(data) {

                            if (data==1){
                                return "积极分子";
                            }
                            if (data==2){
                                return "发展对象";
                            }
                            if (data==3){
                                return "中共预备党员";
                            }
                            if (data==4){
                                return "中共党员";
                            }
                        }
                    },
                    {"mDataProp":"partyName"},
                    {"mDataProp":"applyDate"},
                    {"mDataProp":"state",
                        "mRender": function(data) {
                            if (data==0){
                                return  "<span  class='label label-orange mr10 mb10'>最新提交</span>";
                            }else {
                                return  "<span  class='label label-purple mr10 mb10'>未提交</span><span  class='label label-pink mr10 mb10' id='rollback'>回滚</span>";
                            }
                        }
                    },
                    {"mDataProp":"partyOrganizationNum",
                        "mRender": function(data) {
                            if (data==PON){
                                return  "<span  class='label label-info mr10 mb10' id='update'>修改</span><span  class='label label-danger mr10 mb10' id='delete'>删除</span>";
                            }else {
                                return  "";
                            }
                        }
                    }
                ],
            })
        },
        error:function () {
            layer.msg("网络在开小差~稍后重试");
        }
    });
}

//动态查询
var queryConditions=function(){
    $(document).on('click', '#queryConditions', function () {
        var $queryGrade = $("#queryGrade option:selected").val();
        var $queryParty = $("#queryParty option:selected").val();
        var $queryPartyMemberType = $("#queryPartyMemberType option:selected").val();

        //封装成对象提交
        // var data = {
        //     stuGrade:$queryGrade,
        //     partyMemberType:$queryPartyMemberType,
        //     partyName:$queryParty,
        // };
        console.log("http://localhost:8080/partyMember/queryPartyMemberByConditions?stuGrade=" + $queryGrade + "&partyMemberType=" + $queryPartyMemberType + "&partyName=" + $queryParty);
        $.ajax({
            url: "http://localhost:8080/partyMember/queryPON",
            type: "GET",
            async: false,
            data: {"partyMemberAccount": "1011"},
            dataType: "json",
            contentType: "application/json",
            success: function (data) {
                console.log("data.data"+data.data);
                console.log("data="+data);
                console.log("data.code="+data.code);
                console.log("pon="+data.data.PON);


                var PON = data.data.PON;
                console.log(PON);
                //datatable不能二次初始化，如果不先销毁原datatable，会报错
                var table = $('#dataTable').dataTable()
                if (table) {
                    table.fnDestroy();
                }
                $('#dataTable').dataTable({
                    "bDeferRender": true,
                    "sAjaxDataProp": "data.partyMemberPartyStudentList",
                    "sAjaxSource": "http://localhost:8080/partyMember/queryPartyMemberByConditions?stuGrade=" + $queryGrade + "&partyMemberType=" + $queryPartyMemberType + "&partyName=" + $queryParty,
                    "sPaginationType": "bs_full", //"bs_normal", "bs_two_button", "bs_four_button", "bs_full"
                    "fnPreDrawCallback": function (oSettings) {
                        $('.dataTables_filter input').addClass('form-control input-large').attr('placeholder', 'Search');
                        $('.dataTables_length select').addClass('form-control input-small');
                    },
                    "oLanguage": {
                        "sSearch": "",
                        "sLengthMenu": "<span>_MENU_ entries</span>",
                        "sZeroRecords": "暂无相关信息",
                    },
                    "bJQueryUI": false,
                    "bAutoWidth": false,
                    "sDom": "<'row'<'col-lg-6 col-md-6 col-sm-12 text-center'l><'col-lg-6 col-md-6 col-sm-12 text-center'f>r>t<'row-'<'col-lg-6 col-md-6 col-sm-12'i><'col-lg-6 col-md-6 col-sm-12'p>>",

                    "aoColumns": [
                        {"mDataProp": "stuNum"},
                        {"mDataProp": "stuName"},
                        {
                            "mDataProp": "stuSex",
                            "mRender": function (data) {
                                var value = "男";
                                if (data == 1) {
                                    value = "女";
                                }
                                return value;
                            }
                        },
                        {"mDataProp": "stuNativeplace"},
                        {"mDataProp": "stuBirthday"},
                        {"mDataProp": "stuGrade"},
                        {
                            "mDataProp": "partyMemberType",
                            "mRender": function (data) {

                                if (data == 1) {
                                    return "积极分子";
                                }
                                if (data == 2) {
                                    return "发展对象";
                                }
                                if (data == 3) {
                                    return "中共预备党员";
                                }
                                if (data == 4) {
                                    return "中共党员";
                                }
                            }
                        },
                        {"mDataProp": "partyName"},
                        {"mDataProp": "applyDate"},
                        {"mDataProp":"state",
                            "mRender": function(data) {
                                if (data==0){
                                    return  "<span  class='label label-orange mr10 mb10'>最新提交</span>";
                                }else {
                                    return  "<span  class='label label-purple mr10 mb10'>未提交</span><span  class='label label-pink mr10 mb10' id='rollback'>回滚</span>";
                                }
                            }
                        },
                        {"mDataProp":"partyOrganizationNum",
                            "mRender": function(data) {
                                if (data==PON){
                                    return  "<span  class='label label-info mr10 mb10' id='update'>修改</span><span  class='label label-danger mr10 mb10' id='delete'>删除</span>";
                                }else {
                                    return  "";
                                }
                            }
                        }
                    ],
                })

            },
            error:function () {
                layer.msg("网络在开小差~稍后重试");
            }
        });


    });
}

//删除
$(document).on('click', '#delete', function () {
    var $partyMemberAccount = $(this).parents("tr").children(0).eq(0).text();
    layer.confirm('确定将其移除党组织？',{
        btn:['确认','取消']
    },function () {
        $.ajax({
            url: "http://localhost:8080/partyMember/deletePartyMember",
            type: "GET",
            async: true,
            data: "partyMemberAccount=" + $partyMemberAccount,
            dataType: "json",
            success: function () {
                layer.alert('操作成功!', function(){
                    location.reload();
                });
            },
            error: function () {
                layer.alert("操作失败!");
            }
        });
    })
});

// $(document).on('click', '#delete', function () {
//     //获取$stuNum
//     var $stuNum = $(this).parents("tr").children(0).eq(0).text();
//     $.ajax({
//         url: "http://localhost:8080/student/deleteStudent",
//         type: "GET",
//         async: true,
//         data: "stuNum=" + $stuNum,
//         dataType: "json",
//         success: function () {
//             location.reload();
//             layer.alert("操作成功!");
//         },
//         error: function () {
//             layer.alert("操作失败!");
//         }
//     });
// });

//修改
$(document).on('click', '#update', function () {
    //获取$stuNum
    var $stuNum = $(this).parents("tr").children(0).eq(0).text();
    $.ajax({
        url: "http://localhost:8080/partyMember/queryStudentPoli",
        type: "GET",
        async: true,
        data: "partyMemberAccount=" + $stuNum,
        dataType: "json",
        success: function (data) {
            var $type=data.data.stuPoli;
            console.log("type="+$type);
            //页面层
            layer.open({
                type: 2,
                title: '修改党员身份',
                area: ['800px', '300px'],
                content: ['partyMemberUpdate.html?stuNum='+$stuNum+'&type='+$type, 'no']
            })
        },
        error: function () {
            layer.alert("操作失败!");
        }
    });
});

//回滚
$(document).on('click', '#rollback', function () {
    //获取$stuNum
    var $stuNum = $(this).parents("tr").children(0).eq(0).text();
    //获取最后一行数据，用以判断是否能进行回滚，因为有不是该支部的人也显示有回滚
    var $lastRow = $(this).parents("tr").children(0).eq(10).text();
    console.log("$lastRow="+$lastRow);
    if($lastRow==""){
        layer.alert("您无权限修改非本党支部成员的信息！");
    }else{
        $.ajax({
            url: "http://localhost:8080/partyMember/queryStudentPoli",
            type: "GET",
            async: true,
            data: "partyMemberAccount=" + $stuNum,
            dataType: "json",
            success: function (data) {
                var $type=data.data.stuPoli;
                console.log("type="+$type);

                var ty;
                if ($type == 1) {
                    ty= "积极分子";
                }
                if ($type == 2) {
                    ty= "发展对象";
                }
                if ($type == 3) {
                    ty= "中共预备党员";
                }
                if ($type == 4) {
                    ty= "中共党员";
                }


                var data = {
                    partyMemberAccount:$stuNum,
                    partyMemberType:$type,
                };
                $.ajax({
                    url: "http://localhost:8080/partyMember/rollback",
                    type: "POST",
                    async: false,
                    data:JSON.stringify(data),
                    dataType: "json",
                    contentType:"application/json",
                    success:function (data) {
                        layer.load(1, {
                            shade: [0.1,'#fff'] //0.1透明度的白色背景
                        });
                        if (data.code==1)
                            layer.alert("回滚成功！，学号为"+$stuNum+"的原政治类型为"+ty, function(){
                                location.reload();
                            });
                    },
                    error:function () {
                        layer.msg("网络在开小差~稍后重试");
                    }
                });

            },
            error: function () {
                layer.alert("操作失败!");
            }
        });
    }

});

function getUrlParms(name){
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if(r!=null)
        return unescape(r[2]);
    return null;
}